cmake_minimum_required(VERSION 2.8.3)
project(open_chisel)


#set(CMAKE_BUILD_TYPE "Release")

MESSAGE("Build type: " ${CMAKE_BUILD_TYPE})

#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}  -Wall  -O3 -march=native ")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall   -O3 -march=native")
set( CMAKE_BUILD_TYPE "Debug" )
#set(BUILD_TYPE Release)

# make sure we use Release and warn otherwise
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    message(STATUS "Setting build type to 'Release' as none was specified.")
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
    # Set the possible values of build type for cmake-gui
    set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
            "MinSizeRel" "RelWithDebInfo")
endif()


if (NOT "${CMAKE_BUILD_TYPE}" STREQUAL "Release")
    message(WARNING "CMAKE_BUILD_TYPE not set to 'Release'. Performance may be terrible.")
     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}     -Wall -march=native -Wno-reorder -pthread")
     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -march=native -pthread")
else()
    message(STATUS "Building with build type '${CMAKE_BUILD_TYPE}', turn on the compiler optimization")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}     -Wall -march=native -Wno-reorder -pthread")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -march=native -pthread")
endif()

# Check C++11 or C++0x support
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    add_definitions(-DCOMPILEDWITHC11)
    message(STATUS "Using flag -std=c++11.")
elseif(COMPILER_SUPPORTS_CXX0X)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
    add_definitions(-DCOMPILEDWITHC0X)
    message(STATUS "Using flag -std=c++0x.")
else()
    message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()

LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake-modules)
#find_package(cmake_modules REQUIRED)
find_package(Eigen3 REQUIRED)
include_directories(${Eigen_INCLUDE_DIRS})
find_package(PCL  REQUIRED )

find_package(OpenCV 3.0 QUIET)
if(NOT OpenCV_FOUND)
    find_package(OpenCV 2.4.3 QUIET)
    if(NOT OpenCV_FOUND)
        message(FATAL_ERROR "OpenCV > 2.4.3 not found.")
    endif()
endif()

MESSAGE("Opencv directory"${OpenCV_LIBS})
add_definitions( ${PCL_DEFINITIONS} )
link_directories( ${PCL_LIBRARY_DIRS} )
include_directories(
        ${EIGEN3_INCLUDE_DIR}
        ${PROJECT_SOURCE_DIR}/Thirdparty/sgm/include
        ${PCL_INCLUDE_DIRS}
)


include_directories(include
        util
        )

set(THIRD_PARTY_LIBS
        ${EIGEN3_LIBS}
        ${OpenCV_LIBS}
        ${PROJECT_SOURCE_DIR}/Thirdparty/sgm/lib/libdeeprob_cv_stereo.so
        ${PCL_LIBRARIES}
        )

add_library(${PROJECT_NAME}
        src/Chunk.cpp
        src/ChunkManager.cpp
        src/DistVoxel.cpp
        src/ColorVoxel.cpp
        src/geometry/AABB.cpp
        src/geometry/Plane.cpp
        src/geometry/Frustum.cpp
        src/camera/Intrinsics.cpp
        src/camera/PinholeCamera.cpp
        src/pointcloud/PointCloud.cpp
        src/ProjectionIntegrator.cpp
        src/Chisel.cpp
        src/mesh/Mesh.cpp
        src/marching_cubes/MarchingCubes.cpp
        src/io/PLY.cpp
        src/geometry/Raycast.cpp
        util/KittiReader.cpp
        )

target_link_libraries(${PROJECT_NAME} ${THIRD_PARTY_LIBS})

add_executable(test_kitti
        test/test_kitti.cpp
        test/test_utility.cpp
        )

target_link_libraries(test_kitti
        ${PROJECT_NAME}
        ${THIRD_PARTY_LIBS}
        )

set(USE_CUDA ON)
if (USE_CUDA)
    set(CUDA_TOOLKIT_ROOT_DIR "/usr/local/cuda-7.5")
    find_package(CUDA REQUIRED)
    include_directories(
            ${PROJECT_SOURCE_DIR}/Thirdparty/cuda_sgm/include
    )
    MESSAGE("Has CUDA")
    set(CUDA_ARCH "-arch=sm_50" CACHE STRING "Value of the NVCC -arch option.")

    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)

    CUDA_ADD_EXECUTABLE(test_gpu_sgm_images
            test/test_gpu_sgm_images.cpp
            ${CUDA_SRC}
            )
    TARGET_LINK_LIBRARIES(test_gpu_sgm_images
            ${PROJECT_NAME}
            ${PROJECT_SOURCE_DIR}/Thirdparty/cuda_sgm/lib/libsgm.so
            ${CUDA_LIBRARIES} ${CUDA_nppi_LIBRARY}
            )
endif ()